﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Code.DAL;
using System.Text;

public partial class code1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/plain";
    }

    public string TableName
    {
        get
        {
            return Request["TableName"];
        }
    }

    public string TableDesc
    {
        get
        {
            return CurrentTable.Desc;
        }
    }

    public string TableName_Emoney
    {
        get
        {
            var tbName = TableName.ToLower();
            if (tbName.EndsWith("_msn"))
            {
                return TableName.Substring(0, TableName.Length - 4);
            }
            return TableName;
        }
    }

    public List<Code.DAL.Table> CurrentDables
    {
        get
        {
            return (List<Code.DAL.Table>)Session["Tables"];
        }
    }

    public Code.DAL.Table CurrentTable
    {
        get
        {
            return CurrentDables.Find(x => x.Name == TableName);
        }
    }

    public string GetSpsCol()
    {
        string cols = "";
        for (int i = 0; i < CurrentTable.Columns.Count; i++)
        {
            var c = CurrentTable.Columns[i];
            if(c.IsIdentity || c.Type=="timestamp")
            {
                continue;
            }
            cols += "\"+SqlHelper.SpsValue( c." + c.Name + ")+\",";
        }
        if (cols.Length > 0)
        {
            cols = cols.Substring(0, cols.Length - 1);
        }
        return cols;
    }

    public string GetColumns(string pre)
    {
        string cols = "";
        pre = pre == null ? "" : pre;
        for (int i = 0; i < CurrentTable.Columns.Count; i++)
        {
            var c = CurrentTable.Columns[i];
            if(c.IsIdentity || c.Type=="timestamp")
            {
                continue;
            }
            string strName = c.Name;
            if (string.IsNullOrEmpty(pre))
            {
                strName = "[" + strName + "]";
            }
            cols += pre + strName + ",";
        }
        if (cols.Length > 0)
        {
            cols = cols.Substring(0, cols.Length - 1);
        }
        return cols;
    }

    public string GetKeys()
    {
        var columns = CurrentTable.Columns;
        string str="";
        for (int i = 0; i < columns.Count; i++)
        {
            var c = columns[i];
            if (c.IsKey)
            {
                var name=c.Name;
                name=name.Substring(0,1).ToLower()+name.Substring(1);
                str += ColType.GetColType(c) + " " + name + ",";
            }
        }
        if (str.Length > 0)
        {
            str = str.Substring(0, str.Length - 1);
        }
        return str;
    }

    public string IsExistsColumn()
    {
        var columns = CurrentTable.Columns;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < columns.Count; i++)
        {
            var c = columns[i];
            if (c.IsKey)
            {
                var name = c.Name;
                name = name.Substring(0, 1).ToLower() + name.Substring(1);
                if (sb.Length == 0)
                {
                    sb.AppendLine("tool.AddSqlParameter(\"" + c.Name + "\", " + ColType.GetSQLDBType(c.Type) + "," + name + ");");
                }
                else
                {
                    sb.AppendLine("            tool.AddSqlParameter(\"" + c.Name + "\", " + ColType.GetSQLDBType(c.Type) + "," + name + ");");
                }
            }
        }
        if (sb.Length > 0)
        {
            sb = sb.Remove(sb.Length - 2, 2);
        }
        return sb.ToString();
    }

    public string UpdateColums()
    {
        var columns = CurrentTable.Columns;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < columns.Count; i++)
        {
            var c = columns[i];
            if (c.IsKey  || c.Type=="timestamp")
            {
                continue;
            }

            if (sb.Length==0)
            {
                sb.AppendLine("sb.AppendLine(\"   SET [" + c.Name + "] = \"+SqlHelper.SpsValue(c."+c.Name+"));");
            }
            else
            {
                if (i == columns.Count - 1)
                {
                    sb.Append("            sb.AppendLine(\"      ,[" + c.Name + "] = \"+SqlHelper.SpsValue(c."+c.Name+"));");
                }
                else
                {
                    sb.AppendLine("            sb.AppendLine(\"      ,[" + c.Name + "] = \"+SqlHelper.SpsValue(c."+c.Name+"));");
                }
            }
        }
        return sb.ToString();
    }

    public string IsExistsWhere()
    {
        var columns = CurrentTable.Columns;
        string str = "";
        for (int i = 0; i < columns.Count; i++)
        {
            var c = columns[i];
            if (c.IsKey)
            {
                str += "[" + c.Name + "]=@"+c.Name+" and ";
            }
        }
        if (str.Length > 0)
        {
            str = str.Substring(0, str.Length - 4);
        }
        return str;
    }

    public string UpdateWhere()
    {
        var columns = CurrentTable.Columns;
        string str = "";
        for (int i = 0; i < columns.Count; i++)
        {
            var c = columns[i];
            if (c.IsKey)
            {
                str += "[" + c.Name + "]=\"+SqlHelper.SpsValue(c."+c.Name+")+\" and ";
            }
        }
        if (str.Length > 0)
        {
            str = str.Substring(0, str.Length - 4);
        }
        return str;
    }

     

}